<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>编辑商品</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <link rel="stylesheet" href="css/iconfont.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/companydata.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <!-- 页面  -->
	    <section class="AllContent">
	    	<form>
	    		<div class="FormStyle">
	    			<ul>
	    				<li><i>*</i> 产品名称</li>
	    				<li>
	    					<input type="text" id="name" name="name" value="" placeholder="老坛酸菜"/>
	    				</li>
	    				<li><i>*</i> 价 格</li>
	    				<li>
	    					<input type="text" id="price" name="price" value="" placeholder="5.00元"/>
	    				</li>
	    				<li>产品类别</li>
	    				<li>
	    					<input type="text" id="cat_name" name="cat_name" value="" placeholder="实物"/>
	    				</li>
	    				<li>可否溯源</li>
	    				<li>
	    					<select name="is_tracking" id="is_tracking">
	    						<option value="Y">是</option>
                                <option value="N">否</option>
	    					</select>
	    				</li>
	    				<li>系统编号</li>
	    				<li>
	    					<input type="text" id="outer_id" name="outer_id" value="" placeholder="客户系统编号"/>
	    				</li>
	    				<li>规 格</li>
	    				<li>
	    					<input id="spec" name="spec" value="" placeholder="规格"/>
	    				</li>
	    				<li>单 元</li>
	    				<li>
	    					<input type="text" id="unit" name="unit" value="" placeholder="桶"/>
	    				</li>
	    				<li>产品数量</li>
	    				<li>
	    					<input type="text" id="qty" name="qty" value="" placeholder="999"/>
	    				</li>
	    				<li>有效期</li>
	    				<li>
	    					<input style="display: block;margin-top:4px;line-height:30px;" type="date" id="guarantee_date" name="guarantee_date" value="" placeholder="2099-10-25"/>
	    				</li>
	    				<li>备 注</li>
	    				<li>
	    					<input type="text" id="note" name="note" value="" placeholder="····"/>
	    				</li>
	    				<li>产品描述</li>
	    				<li>
	    					<input id="description" name="description" placeholder="随便说的什么吧..."/>
	    				</li>
	    			</ul>
	    			<div class="FormPhoto">
	    				<img id="pic_url" name="pic_url" src="images/Photo2.png">
	    				<!--<input type="file" id="file_upload"/>-->
	    				<div class="ImageHosting">空间图片</div>
	    			</div>
	    			<p style="margin:0px 4.5%;font-size:0.83em;color:#999;margin-top:10px;font-family: arial;line-height:23px;">上传图片大小不能超过2M，最佳尺寸为200x200px</p>
	    			
                    <input type="text" id="pid" name="pid" value=""  style="display:none"/>
	    			<button class="NextButton" type="button" onclick="saveproduct();">保存</button>
	    		</div>
	    	</form>
	    	
	    	<div class="SlidingLayer">
	    		<div style="overflow: hidden;background-color:#fff;">
	    			<span class="ImageHostingTitle">空间图片</span>
	    			<img class="CloseImg" style="width:30px;" src="images/remover.png">
	    		</div>
	    		<ul id="piclist">
	    			
	    		</ul>
	    	</div>
	    </section>
	    
	    <section>
	    	<div class="MaskLayer">
		    	<div class="MaskContent">
		    		<p><img src="images/loading5.gif"></p>
		    		<p>正在添加...</p>
		    	</div>
	    	</div>
	    </section>
		
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init();
		$('.AllHeader li:eq(1)').text('编辑商品')

		$(function () {
	        $("#file_upload").change(function() {
				var $file = $(this);
				var fileObj = $file[0];
				var windowURL = window.URL || window.webkitURL;
				var dataURL;
				var $img = $("#pic_url");
				 
				if(fileObj && fileObj.files && fileObj.files[0]){
				dataURL = windowURL.createObjectURL(fileObj.files[0]);
				$img.attr('src',dataURL);
				}else{
				dataURL = $file.val();
				var imgObj = document.getElementById("pic_url");
				imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
				 
				}
			});
			
	        var pid = $_GET['pid'];
	        $.ajax({
	            type: "post",
	            url: getServerUrl() + "/Open/Company/copyproduct",
	            dataType: 'json',
	            data: 'access_token=' + getToken()+'&pid='+pid,
	            success: function (json) {
	                $("#pid").val(json.pid);
	                $("#name").val(json.name);
	                $("#outer_id").val(json.outer_id);
	                $("#cat_name").val(json.cat_name);
	                $("#price").val(json.price);
	                $("#spec").val(json.spec);
	                $("#description").val(json.description);
	                $("#is_tracking").val(json.is_tracking);
	                $("#unit").val(json.unit);
	                $("#qty").val(json.qty);
	                $("#guarantee_date").val(json.guarantee_date);
	                $("#pic_url").val(json.pic_url);
					$('#pic_url').attr("src",json.pic_url);
	                $("#note").val(json.note);
	            },
	            error: function () {
	                alertMessage("请检查网络连接！");
	            }
	        });
	        getpic('全部',1);
	   
	        var winheight = $(window).height();
	        $('.SlidingLayer').height(winheight-56);
		});
	
	    function onDeviceReady() {
	        //document.addEventListener("backbutton", onBackKeyDown, false);
	    }
	    function onBackKeyDown() {
	        //openUrl("/Desktop/index");
	    }
	
	    function saveproduct() {
	        var pid = $("#pid").val();
	        var name = $("#name").val();
	        var outer_id =$("#outer_id").val();
	        var cat_name =$("#cat_name").val();
	        var price =$("#price").val();
	        var spec =$("#spec").val();
	        var description =$("#description").val();
	        var is_tracking =$("#is_tracking").val();
	        var unit = $("#unit").val();
	        var qty = $("#qty").val();
	        var guarantee_date =$("#guarantee_date").val();
	        var pic_url = $('#pic_url').attr("src");
	        var note =$("#note").val();
	        if (empty(pid)) {
	            alertMessage("产品id不能为空！");
	            return;
	        }
	        if (empty(name)) {
	            alertMessage("产品名称不能为空！");
	            return;
	        }
	        var datas = {access_token:getToken(),pid:pid,name:name,outer_id:outer_id,cat_name:cat_name,price:price,spec:spec,description:description,
	            is_tracking:is_tracking,unit:unit,qty:qty,guarantee_date:guarantee_date,pic_url:pic_url,note:note};
	        $.ajax({
	            type: "post",
	            url: getServerUrl() + "/Open/Company/editproduct",
	            dataType: 'json',
	            data: datas,
	            success: function (json) {
	                if (json.result == "1") {
						window.location.href= 'ProductsList.html';
	                } else {
	                    alertMessage("编辑产品失败:[" + json.error_description + "]");
	                }
	            }
	        });
	    }
	</script>
</html>
